<?php
declare(strict_types=1);

namespace App\Model;

use Carbon\Carbon;
use Hyperf\Database\Model\SoftDeletes;

/**
 * @property int $id
 * @property int $type_id
 * @property int $menu_id
 * @property int $main_id
 * @property int $page_number
 * @property int $top_number
 * @property int $sort_number
 * @property int $status
 * @property int $viewed_count
 * @property int $liked_count
 * @property string $tag_ids
 * @property string $title
 * @property string $title_short
 * @property string $source_from
 * @property string $image_cover
 * @property string $image_inner
 * @property string $image_link
 * @property int $content_ide
 * @property string $content_short
 * @property string $content
 * @property Carbon $enable_start
 * @property Carbon $enable_end
 * @property Carbon $created_at
 * @property Carbon $updated_at
 * @property string $deleted_at
 * @property string $type_name
 * @property string $status_name
 * @property string $ide_name
 * @property string $menu_name
 * @property string $tag_names
 * @property int $locked_by
 * @property string $locked_by_admin
 */
class ContentItem extends MyBaseModel
{
    const TYPE_NAMES = [
        0 => '文章',
        1 => '图片Banner',
        2 => '文字链接',
    ];

    const STATUS_NAMES = [
        0 => '待填写',
        1 => '待审核',
        2 => '待修订',
        3 => '待发布',
        4 => '已发布',
        5 => '已下线',
    ];

    const IDE_NAMES = [
        0 => 'Textarea',
        1 => 'WangEditor',
        2 => 'Markdown',
    ];

    use SoftDeletes;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'content_item';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'id' => 'integer',
        'type_id' => 'integer',
        'menu_id' => 'integer',
        'main_id' => 'integer',
        'page_number' => 'integer',
        'top_number' => 'integer',
        'sort_number' => 'integer',
        'status' => 'integer',
        'viewed_count' => 'integer',
        'liked_count' => 'integer',
        'content_ide' => 'integer',
        'enable_start' => 'datetime',
        'enable_end' => 'datetime',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    /**
     * 扩展字段
     * @var string[]
     */
    protected $appends = [
        'type_name',
        'menu_name',
        'status_name',
        'ide_name',
        'tag_names',
        'locked_by_admin',
    ];

    public function getTagNamesAttribute(): string
    {
        if (is_null($this->tag_ids ?? null)) {
            return '';
        }
        $names = ContentTag::getNamesByIds($this->tag_ids);
        return implode(',', $names);
    }

    public function getMenuNameAttribute(): string
    {
        return static::getMenuName(intval($this->menu_id));
    }

    public static function getMenuName(?int $menuId): string
    {
        if (!$menuId) {
            return '未关联栏目';
        }
        /* @var ContentMenu $menuModel */
        $menuModel = ContentMenu::query(true)->find($menuId);
        return $menuModel ? $menuModel->name : '未关联栏目';
    }

    public function getTypeNameAttribute(): string
    {
        return static::getTypeName(intval($this->type_id));
    }

    public static function getTypeName(?int $typeId): string
    {
        if (is_null($typeId)) {
            return '未知';
        }
        return static::TYPE_NAMES[$typeId] ?? '未知';
    }

    public function getStatusNameAttribute(): string
    {
        return static::getStatusName(intval($this->status));
    }

    public static function getStatusName(?int $status): string
    {
        if (is_null($status)) {
            return '未知';
        }
        return static::STATUS_NAMES[$status] ?? '未知';
    }

    public function getIdeNameAttribute(): string
    {
        if (is_null($this->content_ide ?? null)) {
            return '未知';
        }
        return static::IDE_NAMES[$this->content_ide] ?? '未知';
    }

    public function getLockedByAdminAttribute(): string
    {
        return static::getLockedByAdmin(intval($this->locked_by));
    }

    public static function getLockedByAdmin(?int $lockedBy): string
    {
        if ($lockedBy <= 0 || is_null($lockedBy)) {
            return '';
        }
        $adminUser = AdminUser::find($lockedBy);
        if (!$adminUser) {
            return '';
        }
        return $adminUser->name;
    }
}
